﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using MvcEngine.Core;

namespace MvcEngine.Mvc.View.Skin
{
    public class DefaultSkinEngine : ISkinEngine
    {
        #region Static protected fields

        //0 - Name
        //1 - Controller / Folder name
        //2 - Skin name
        //3 - Site name
        //4 - Module name
        //5 - Template name
        //6 - Area Name

        protected static readonly string[] masterLocationFormats = new[]
        {
            "~/Skins/{2}/Views/Shared/{0}.master",
            "~/Skins/{2}/Views/{0}.master",

            "~/Areas/{6}/Views/{1}/{0}.master",
            "~/Areas/{6}/Views/Shared/{0}.master",
            "~/Views/Shared/{0}.master"
        };

#warning change this so modules views should be looked at first
        protected static readonly string[] viewLocationFormats = new[]
        {
            "~/Areas/{6}/Views/{1}/Controls/{0}.ascx",
            "~/Areas/{6}/Views/{1}/{0}.aspx",
            "~/Areas/{6}/Views/{1}/{0}.ascx",
            "~/Areas/{6}/Views/{0}.ascx",
            "~/Areas/{6}/Views/Shared/{0}.ascx",
            "~/Views/Controls/{0}.ascx",

            "~/Skins/{2}/Views/{1}/{0}.aspx",
            "~/Skins/{2}/Views/{1}/{0}.ascx",
            "~/Skins/{2}/Views/Shared/{0}.aspx",
            "~/Skins/{2}/Views/Shared/{0}.ascx",

            "~/Skins/{2}/Templates/{5}/{6}/Views/{0}.aspx",
            "~/Skins/{2}/Templates/{5}/{6}/Views/{0}.ascx",


            "~/Views/Shared/{0}.ascx",
            "~/Views/{0}.ascx",
            "~/Modules/{4}/Areas/{6}/Views/{1}/{0}.aspx",
            "~/Modules/{4}/Areas/{6}/Views/{1}/{0}.ascx",
            "~/Modules/{4}/Areas/{6}/Views/{1}/Controls/{0}.ascx",
            "~/Modules/{4}/Areas/{6}/Views/Shared/{0}.ascx",
            
        };

        #endregion

        #region Constructors

        public DefaultSkinEngine()
        {

        } 

        #endregion

        #region Public methods
        
        public virtual string FindMasterPath(string name, string areaName, string folderName, string moduleName, out string[] locationsSearched)
        {
            return FindPath(masterLocationFormats, name, areaName, folderName, moduleName, out locationsSearched);
        }

        public virtual string FindViewPath(string name, string areaName, string folderName, string moduleName, out string[] locationsSearched)
        {
            return FindPath(viewLocationFormats, name, areaName, folderName, moduleName, out locationsSearched);
        }

        public virtual string FindPartialViewPath(string name, string areaName, string folderName, string moduleName, out string[] locationsSearched)
        {
            return FindPath(viewLocationFormats, name, areaName, folderName, moduleName, out locationsSearched);
        }

        #endregion

        #region Protected methods

        protected string FindPath(string[] locationsToSearch, string name, string areaName, string folderName, string moduleName, out string[] locationsSearched)
        {
            int locationCount = 0;
            locationsSearched = new string[locationsToSearch.Length];

            foreach (string locationFormat in locationsToSearch)
            {
                string path = string.Format(locationFormat, name, folderName, SiteContext.Current.Skin.Name, SiteContext.Current.Site.Name, moduleName, SiteContext.Current.SkinTemplate.Name, areaName);
                locationsSearched[locationCount++] = path;

                string path1 = HostingEnvironment.VirtualPathProvider.GetFile(path).VirtualPath;

                if (HostingEnvironment.VirtualPathProvider.FileExists(path))
                {
                    return path;
                }
            }

            return null;
        } 

        #endregion
    }
}
